package com.brainhealth.customer.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.convert.Convert;
import com.brainhealth.common.enums.GoodsType;
import com.brainhealth.common.enums.YNEnum;
import com.brainhealth.common.utils.DateUtils;
import com.brainhealth.customer.domain.Customer;
import com.brainhealth.customer.domain.RoomUserProperty;
import com.brainhealth.customer.domain.ScoreMallGoodsCategory;
import com.brainhealth.customer.mapper.RoomUserPropertyMapper;
import com.brainhealth.customer.mapper.ScoreMallGoodsCategoryMapper;
import com.brainhealth.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.brainhealth.customer.mapper.ScoreMallGoodsMapper;
import com.brainhealth.customer.domain.ScoreMallGoods;
import com.brainhealth.customer.service.IScoreMallGoodsService;

import javax.servlet.http.HttpServletRequest;

/**
 * 商城商品信息Service业务层处理
 * 
 * @author wolong
 * @date 2023-11-01
 */
@Service
public class ScoreMallGoodsServiceImpl implements IScoreMallGoodsService 
{
    @Autowired
    private ScoreMallGoodsMapper scoreMallGoodsMapper;
    @Autowired
    private ScoreMallGoodsCategoryMapper scoreMallGoodsCategoryMapper;
    @Autowired
    private RoomUserPropertyMapper roomUserPropertyMapper;

    /**
     * 查询商城商品信息
     * 
     * @param id 商城商品信息主键
     * @return 商城商品信息
     */
    @Override
    public ScoreMallGoods selectScoreMallGoodsById(Long id)
    {
        return scoreMallGoodsMapper.selectScoreMallGoodsById(id);
    }

    @Override
    public ScoreMallGoods selectScoreMallGoodsByCustomerId(HttpServletRequest request, Long goodsId)
    {
        Customer customer = CommonUtils.getCustomerByRequest(request);
        ScoreMallGoods goods = scoreMallGoodsMapper.selectScoreMallGoodsByGoodsId(goodsId);
        RoomUserProperty roomUserProperty = new RoomUserProperty();
        roomUserProperty.setCustomerId(customer.getCustomerId());
        roomUserProperty.setYn(YNEnum.NO.getCode());
        List<RoomUserProperty> lstCustomerProperty = roomUserPropertyMapper.selectRoomUserPropertyList(roomUserProperty);
        RoomUserProperty customerGoods = lstCustomerProperty.stream().filter(item -> item.getGoodsId().equals(goodsId)).findFirst().orElse(null);
        if (customerGoods != null && !customerGoods.getGoodsType().equals(GoodsType.AIPackage.getCode()))
        {
            goods.setHave(true);
        } else
        {
            goods.setHave(false);
        }
        return goods;
    }

    @Override
    public List<ScoreMallGoods> getMallGoodsByCategoryId(Long categoryId)
    {
        ScoreMallGoods query = new ScoreMallGoods();
        query.setCategoryId(categoryId);
        return scoreMallGoodsMapper.selectScoreMallGoodsList(query);
    }

    @Override
    public ScoreMallGoods getAIPackageGoods()
    {
        ScoreMallGoods query = new ScoreMallGoods();
        query.setGoodsType(GoodsType.AIPackage);
        List<ScoreMallGoods> lstGoods = scoreMallGoodsMapper.selectScoreMallGoodsList(query);
        if (lstGoods.size()> 0)
        {
            return lstGoods.get(0);
        }
        return null;
    }

    /**
     * 查询商城商品信息列表
     * 
     * @param scoreMallGoods 商城商品信息
     * @return 商城商品信息
     */
    @Override
    public List<ScoreMallGoods> selectScoreMallGoodsList(ScoreMallGoods scoreMallGoods)
    {
        if (scoreMallGoods.getCategoryId() != null)
        {
            ScoreMallGoodsCategory query = new ScoreMallGoodsCategory();
            List<ScoreMallGoodsCategory> lstCategory = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(query);
            ScoreMallGoodsCategory category = lstCategory.stream().filter(item -> item.getCategoryId().equals(scoreMallGoods.getCategoryId())).findFirst().orElse(null);
            List<Long> lstChilds = new ArrayList<>();
            getChildNodeList(category, lstCategory, lstChilds);
            lstChilds.add(category.getCategoryId());
            Long[] ids = Convert.toLongArray(lstChilds);
            List<ScoreMallGoods> lstCategoryGoods = scoreMallGoodsMapper.selectScoreMallGoodsListByCategoryIds(ids);
            return lstCategoryGoods;
        }
        return scoreMallGoodsMapper.selectScoreMallGoodsList(scoreMallGoods);
    }

    /**
     * 获取当前节点的子节点
     * @param node
     * @param lstAllData
     * @param lstIds
     */
    public void getChildNodeList(ScoreMallGoodsCategory node,List<ScoreMallGoodsCategory> lstAllData, List<Long> lstIds)
    {
        List<ScoreMallGoodsCategory> lstChild = lstAllData.stream().filter(item->item.getParentCategoryId().equals(node.getCategoryId())).collect(Collectors.toList());
        for (ScoreMallGoodsCategory child: lstChild)
        {
            lstIds.add(child.getCategoryId());
            getChildNodeList(child, lstAllData, lstIds);
        }
    }

    /**
     * 新增商城商品信息
     * 
     * @param scoreMallGoods 商城商品信息
     * @return 结果
     */
    @Override
    public int insertScoreMallGoods(ScoreMallGoods scoreMallGoods)
    {
        scoreMallGoods.setCreateTime(DateUtils.getNowDate());
        Long goodsCount = scoreMallGoodsMapper.selectScoreMallGoodsCount();
        scoreMallGoods.setGoodsId(goodsCount);
        scoreMallGoods.setYn(0);
        ScoreMallGoodsCategory queryCategory = new ScoreMallGoodsCategory();
        queryCategory.setCategoryId(scoreMallGoods.getCategoryId());
        List<ScoreMallGoodsCategory> lstCategory = scoreMallGoodsCategoryMapper.selectScoreMallGoodsCategoryList(queryCategory);
        if (lstCategory.size() > 0)
        {
            scoreMallGoods.setLayer(lstCategory.get(0).getLayer());
        }
        return scoreMallGoodsMapper.insertScoreMallGoods(scoreMallGoods);
    }

    /**
     * 修改商城商品信息
     * 
     * @param scoreMallGoods 商城商品信息
     * @return 结果
     */
    @Override
    public int updateScoreMallGoods(ScoreMallGoods scoreMallGoods)
    {
        scoreMallGoods.setUpdateTime(DateUtils.getNowDate());
        return scoreMallGoodsMapper.updateScoreMallGoods(scoreMallGoods);
    }

    /**
     * 批量删除商城商品信息
     * 
     * @param ids 需要删除的商城商品信息主键
     * @return 结果
     */
    @Override
    public int deleteScoreMallGoodsByIds(Long[] ids)
    {
        return scoreMallGoodsMapper.deleteScoreMallGoodsByIds(ids);
    }

    /**
     * 删除商城商品信息信息
     * 
     * @param id 商城商品信息主键
     * @return 结果
     */
    @Override
    public int deleteScoreMallGoodsById(Long id)
    {
        return scoreMallGoodsMapper.deleteScoreMallGoodsById(id);
    }
}
